/*
 * 文件名：WebConfig.java
 * 版权：MoonBox 
 * 修改人：Peter
 * 修改时间：2018年10月10日
 * 修改内容：新增
 */
package com.renzhebiao.lkang.filter;

import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import com.renzhebiao.lkang.request.EventLogJackson2HttpMessageConverter;
import com.renzhebiao.lkang.utils.Jsons;

/**
 * <p>
 * 
 * @author Peter
 * @since 1.0.0
 */
@Configuration
public class WebConfig {

    @Bean
    public FilterRegistrationBean requestFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        // 注入过滤器
        registration.setFilter(new RequestFilter());
        // 拦截规则
        registration.addUrlPatterns("/*");
        // 过滤器名称
        registration.setName("RequestFilter");
        // 过滤器顺序
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
        return registration;
    }

    @Bean
    public FilterRegistrationBean corsFilter() {
        FilterRegistrationBean registration = new FilterRegistrationBean();
        // 注入过滤器
        registration.setFilter(new CorsFilter());
        // 拦截规则
        registration.addUrlPatterns("/*");
        // 过滤器名称
        registration.setName("CorsFilter");
        // 过滤器顺序
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registration;
    }

    @Bean // 暂时不知道做什么
    public HttpMessageConverters jacksonJsonHttpMessageConverters() {
        MappingJackson2HttpMessageConverter jsonConverter = new EventLogJackson2HttpMessageConverter();
        jsonConverter.setObjectMapper(Jsons.getObjectMapper());
        HttpMessageConverter<?> converter = jsonConverter;
        return new HttpMessageConverters(converter);
    }
}
